package com.fenqing.leetcode.code.t1200_t1300;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.*;

/**
 * @author fenqing
 */
public class Topic1282 extends Topic {

    @Execute
    public List<List<Integer>> groupThePeople(int[] groupSizes) {
        Map<Integer, List<Integer>> map = new HashMap<>();
        for (int i = 0; i < groupSizes.length; i++) {
            int num = groupSizes[i];
            List<Integer> integers = map.computeIfAbsent(num, k -> new ArrayList<>());
            integers.add(i);
        }
        Arrays.sort(groupSizes);
        List<List<Integer>> result = new ArrayList<>();
        for (int i = 0; i < groupSizes.length; i++) {
            int num = groupSizes[i];
            List<Integer> idx = map.get(num);
            for (int j = 0; j < idx.size(); j++) {
                if (j % num == 0) {
                    result.add(new ArrayList<>());
                }
                List<Integer> integers = result.get(result.size() - 1);
                integers.add(idx.get(j));
            }
            i += idx.size() - 1;
        }
        return result;
    }

    public static void main(String[] args) {
        Topic1282 topic1282 = new Topic1282();
        List<List<Integer>> run = topic1282.run("[3,3,3,3,3,1,3]");
        System.out.println(run);
    }

}
